﻿using System;
using System.Collections.Generic;
using System.Web.Routing;
using System.Web.Mvc;
using Oxite.Infrastructure;
using Oxite.Configuration;
using Microsoft.Practices.Unity;
using ECube.HR.Services;
//using ECube.HR.Repositories;
//using ECube.HR.Repositories.SqlServer;
namespace ECube.HR
{
    public class ECubeHRModule : IOxiteModule, IOxiteDataProvider
    {
        private readonly string[] controllerNamespaces;
        private readonly IUnityContainer container;

        public ECubeHRModule(IUnityContainer container)
        {
            controllerNamespaces = new[] { "ECube.HR.Controllers" };
            this.container = container;
        }

        #region IOxiteModule Members

        public void Initialize(OxiteModuleConfigurationElement module)
        {
        }
        public void Unload()
        {
        }

        public void RegisterRoutes(RouteCollection routes)
        {
           routes.MapRoute(
                "AdminHR",
                "Admin/HR/{action}/{id}",
                new { controller = "HR", action = "Manage", id = "" }, 
                null, 
                controllerNamespaces
                );           
        }

        public void RegisterCatchAllRoutes(RouteCollection routes)
        {               
        }

        public void RegisterFilters(IFilterRegistry filterRegistry)
        {
            //filterRegistry.Add(Enumerable.Empty<IFilterCriteria>(), typeof(ContentItemFilter));

            //ControllerActionFilterCriteria contentItemsInputCriteria = new ControllerActionFilterCriteria();
            //contentItemsInputCriteria.AddMethod<PageController>(p => p.Edit(null, null));
            //contentItemsInputCriteria.AddMethod<PageController>(p => p.SaveEdit(null, null));
            //filterRegistry.Add(new[] { contentItemsInputCriteria }, typeof(ContentItemsInputFilter));

            //ControllerActionFilterCriteria pageListActionCriteria = new ControllerActionFilterCriteria();
            //pageListActionCriteria.AddMethod<PageController>(p => p.Add(null));
            //pageListActionCriteria.AddMethod<PageController>(p => p.AddSave(null));
            //pageListActionCriteria.AddMethod<PageController>(p => p.Edit(null, null));
            //pageListActionCriteria.AddMethod<PageController>(p => p.SaveEdit(null, null));
            //filterRegistry.Add(new[] { pageListActionCriteria }, typeof(PageListActionFilter));

            //ControllerActionFilterCriteria adminActionsCriteria = new ControllerActionFilterCriteria();
            //adminActionsCriteria.AddMethod<PageController>(p => p.Add(null));
            //adminActionsCriteria.AddMethod<PageController>(p => p.AddSave(null));
            //adminActionsCriteria.AddMethod<PageController>(p => p.Edit(null, null));
            //adminActionsCriteria.AddMethod<PageController>(p => p.SaveEdit(null, null));
            //adminActionsCriteria.AddMethod<PageController>(p => p.Remove(null, null));
            //filterRegistry.Add(new[] { adminActionsCriteria }, typeof(AuthorizationFilter));
        }

        public void RegisterModelBinders(ModelBinderDictionary modelBinders)
        {
            //modelBinders[typeof(PageInput)] = new PageInputModelBinder();
            //modelBinders[typeof(Page)] = container.Resolve<PageModelBinder>();
            //modelBinders[typeof(MvcSiteMapNodeInput)] = new MvcSiteMapNodeInputModelBinder();
            //modelBinders[typeof(ContentItemInput)] = new ContentItemInputModelBinder();
            //modelBinders[typeof(ContentItemsInput)] = new ContentItemsInputModelBinder();
        }

        public void RegisterWithContainer()
        {
            container
                .RegisterType<IHRService, HRService>();
            //    .RegisterType<IValidator<PageInput>, PageInputValidator>();
            //    .RegisterType<IContentItemService, ContentItemService>();
        }

        #endregion

        #region IOxiteDataProvider Members

        public void ConfigureProvider(OxiteConfigurationSection config, OxiteDataProviderConfigurationElement dataProviderConfig, IUnityContainer container)
        {
            //if (dataProviderConfig.Category == "LinqToSql")
            //    container
            //        .RegisterType<OrgDBDataContext>(new InjectionConstructor(new ResolvedParameter<string>(!string.IsNullOrEmpty(dataProviderConfig.ConnectionString) ? dataProviderConfig.ConnectionString : config.Providers.DefaultConnectionString)))
            //        .RegisterType<IOrganizationRepository, SqlServerOrganizationRepository>();
            //        .RegisterType<IContentItemRepository, SqlServerContentItemRepository>();
        }

        #endregion
    }
}
